package com.xzh.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.xzh.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000121646343";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC32UG8M2iVr6vqN4IEUDIzQW4DJAGZzukiWHc8MkVqu7TmZvuIwje2OsbVokBnOa78urYX95jNEh00GT17u2AXdCBEeQJmAF9ia0s5bLc9hGMqmm8K/5DChzYrm8SgguUROTPMHHKqR+9U2UCZc7lVIXLVohTy/B0YtJsaRazk4a2rYS/rtPHtOmd385hNfRKpCWGBtB1kSG1sPM9CcjppW4xeqYGIcqGxoJEW6jSscufTuTxxiQec3xfP47FlrbC/cBLGWh03q23vPpxWVB+2HI0nf4sRSmfcXixtwou/jDDwzO0no480GYbesZSK2hH5UeFjDR0N+9zoCJRcTTeDAgMBAAECggEAfv6Nz8jbF3rPj+xj3ULgSlOKAJVgnbQ1ikn9oxE+rimXHghI/ADcpv4W1v9K6YMJAv6eRhP78rvufExrEgsup0TJ/UJxi6/6M2vV/IbCVLYcIhOnXLuIbTclDh59RjrkrG8M/pMwCbvC8KNZYiZ0AQAn6LYbLnVETAqFkTJL6ZVmiBPGewWGjHu8mFYxbtS3W/l+3gXSn2s7gdyQ1H92TUujOIwajHTfgadBcI0lQ5P8ZRPYjMnivGdsYzu3HUfmuAH/k7PVqVPWUwz7GFPhcGfqHuAid9gXIBBeRyRqNO+67p0XpvGDgzUNGquVR1L/s1g8tAWo6aLG2CooyX+s0QKBgQDhZlHMGzC+9hZzxytt3gWLAtr8YCYEhjJUGZkEWmdjkRzYxeoPob8U9Bp8NfsP0Fucx0NiPs/+6JG+wpLQ7ytpMGZChU39N66xzlzUKlxx/tiamLNWtOUxYxBZ3lQXhVVNeeVD8z53qRWUl0CCoKgjkNg0PI/QvtzPKgGNnO9JtwKBgQDQztjck8sdXQel/bI5z6VHBTaxF1mAmzv1k8jXCTOM6FEQ4TymnRDVjPRkbwdWQeJuRebuVmcyuBFD3WUtF+JnC8tCvBpEGDosHT59DiBclpVacj0alfLI7NOMjf2+OHYzw1CJTJduCYZkq03rRmc6qFqkRUXisJG+rj6TRnMwlQKBgBOSqgm6VrpOblaxdfD9dafDoLaHtuVvrdElw+bf3tiDkmi0jwW6evhb1izxFxMhecMcfpp6b99n3Lge91bTXgatYEPohYQ2oKHqFJ+L6ikwAykxl2kxHjHys0nKh6zfNDAdJK6I4eTMNwaHN9/gCZnFCzFGDQsId+45bJcaiKDzAoGAeifIbqJ3vSHji5QzY+a6RiYirM+AKOCn3y8p28MiSVjPtBRQqxhP9N6VSvw5SpkYonJ6ewxJKz7LuuwWeJ70cFxR+SSaKSQTRCOHnxlYjVH65gLXBGqcsjb6O8wFmfbmD7o/r1MDc/hWiyY8W1vAoGxFOrzb+ZZabTb6nKYmVVECgYASCymcAaD3qOmcEWCGfle4V6bhMHENmGwrXn0bw8lLt/EIuIc82vc4YTVE64oKiLK4ONeiHz5m7mXk5SE9ysqjdzkRO4/OnvCyrsqZBoez97dwbFX1ncbJlDDT0zRSdpPs2TzoRsu0hgvubbxI/hC1XudoZlpecrL0RU5+az+X/g==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key =  "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsyjM3P1dmQT38cCoixRqFUD4mg9Bw0iRAO0Dxnft6OmZi/GGv3dXLN1qrCWl+4B3HiYWepPA2kuF06fipv9z+f2Y+//jaJ9SZdpM4jg+qmT+OCm0CsfTOnRfwKdF7dm/jfV7exJtA2OgMQv+tnXpx1E0VcZ9Xclg7snRO8cLNjnmp56YuRdecfcnqo4dvdt77ecRQzmzDFUTcPklo0L7zfdu3PmgnYjQt3dLRy5THxcI7+EfnsSzB2uY5KLcFpiEpo19XMlY9TkxtrgWmZaV0whf/fGvR+9ys/YrPLLjeRe8SBstBH4G34dJqU9Cx6B3LBYtw1yDlyvVW+urjXRb0wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "https://6021-218-77-77-91.ngrok.io/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.xzhmall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeOut = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeOut+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
